//
//  SLFriendViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/5/9.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLFriendViewController.h"
#import "UIButton+SomeMethodOfButton.h"
#import "SLFriendViewModel.h"
#import "SLFriendListTableViewCell.h"
#import "SLAddNewFriendViewController.h"
#import "SLHisBoatsViewController.h"
#import "SLHisGoodsViewController.h"
#import "SLFriendManager.h"

@interface SLFriendViewController ()<UITableViewDataSource, UITableViewDelegate,SLFriendListTableViewCellDelegate>

@property (nonatomic, strong) SLFriendViewModel *friendVM;

@property (nonatomic, strong) UITableView *friendListTableview;

@property (nonatomic, strong) UIButton *addFbtn;

@end

@implementation SLFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  [self.friendVM getBuddylist];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
  
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  
}

#pragma mark - Private Methods

- (void)addObservers{
  @weakify(self)
  [[RACObserve(self.friendVM, friendList) filter:^BOOL(NSMutableArray *value) {
    return value.count > 0;
  }] subscribeNext:^(id x) {
    @strongify(self)
    [self.friendListTableview reloadData];
    [SVProgressHUD dismiss];
  }];
  
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kFriendChange object:nil] subscribeNext:^(id x) {
    @strongify(self)
    [self.friendVM getBuddylist];
  }];
  
  
}

- (void)addNewFriend{
  SLAddNewFriendViewController *addNewVC = [SLAddNewFriendViewController new];
  [self.tabBarController.navigationController pushViewController:addNewVC animated:YES];
}


- (void)addConstrains{
  
  [self.addFbtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.centerX.equalTo(self.view);
    make.height.equalTo(@(50*MIX_W));
    make.top.equalTo(self.naviHeader.mas_bottom).offset(8);
  }];
  
  [self.friendListTableview mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.centerX.equalTo(self.view);
    make.top.equalTo(self.addFbtn.mas_bottom).offset(8);
    make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
  }];
}

- (void)setUpUI{
  [self.view addSubview:self.addFbtn];
  [self.view addSubview:self.friendListTableview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无好友" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

#pragma mark - SLFriendListTableViewCellDelegate

- (void)didLongPressCellWithModel:(SLFriendModel *)model{
  @weakify(self)
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除好友吗?" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    @strongify(self)
    [self deletFriendWithModel:model];
  }];
  
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    [alert dismissViewControllerAnimated:YES completion:nil];
  }];
  
  [alert addAction:action];
  [alert addAction:cancelAction];
  [self presentViewController:alert animated:YES completion:nil];
  
}

- (void)deletFriendWithModel:(SLFriendModel *)model{
  SLFriendManager *mng = [[SLFriendManager alloc] init];
  @weakify(self)
  [SVProgressHUD showWithStatus:@"正在删除"];
  [mng operFriendWithHuid:model.fid opers:@"-1" block:^{
    @strongify(self)
    [self.friendVM getBuddylist];
  }];

}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{
  DLog(@"点击了。。。。。。。。。");
  [self.friendVM getBuddylist];
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.friendVM.friendList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  SLFriendListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  cell.delegate = self;
  return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  return 100;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SLFriendListTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  [cell configFriendListCellWithModel:self.friendVM.friendList[indexPath.row]];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLFriendModel *model = self.friendVM.friendList[indexPath.row];
  if (model.type == 0) {
    SLHisBoatsViewController *boatsVC = [SLHisBoatsViewController new];
    [boatsVC getMoreBoatWithHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
    [self.tabBarController.navigationController pushViewController:boatsVC animated:YES];
  }else{
    SLHisGoodsViewController *goodsVC = [SLHisGoodsViewController new];
    [goodsVC changeHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
    [self.tabBarController.navigationController pushViewController:goodsVC animated:YES];
  }
}




#pragma mark - getter
- (UITableView *)friendListTableview{
  if (!_friendListTableview) {
    _friendListTableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_friendListTableview registerNib:[UINib nibWithNibName:@"SLFriendListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _friendListTableview.tableFooterView = [UIView new];
    _friendListTableview.delegate = self;
    _friendListTableview.dataSource = self;
    _friendListTableview.emptyDataSetSource = self;
    _friendListTableview.emptyDataSetDelegate = self;
  }
  return _friendListTableview;
}

- (UIButton *)addFbtn{
  if (!_addFbtn) {
    _addFbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_addFbtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [_addFbtn setTitle:@"＋添加新朋友" forState:UIControlStateNormal];
    [_addFbtn setTintColor:SLYellowColor];
    [_addFbtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _addFbtn.backgroundColor = [UIColor whiteColor];
    _addFbtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [_addFbtn addTarget:self action:@selector(addNewFriend) forControlEvents:UIControlEventTouchUpInside];
//    [_addFbtn dw_imageTitleHorizontalAlignmentWithSpace:4];
  }
  return _addFbtn;
}

- (SLFriendViewModel *)friendVM{
  if (!_friendVM) {
    _friendVM = [SLFriendViewModel new];
  }
  return _friendVM;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
